package us.kg.kotoriforest.springmvc.restful.dao.impl;

import lombok.AllArgsConstructor;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import us.kg.kotoriforest.springmvc.restful.dao.EmployeeDao;
import us.kg.kotoriforest.springmvc.restful.pojo.Employee;

import java.util.List;

/**
 * @author Kotori rotroutine@163.com
 * @since 2025/2/4
 */
@Repository
@AllArgsConstructor
public class EmployeeDaoImpl implements EmployeeDao {
    private final JdbcTemplate jdbcTemplate;

    @Override
    public Employee getEmployeeById(long id) {
        String sql = "SELECT * FROM employee WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Employee.class), id);
    }

    @Override
    public int addEmployee(Employee employee) {
        String sql = "INSERT INTO employee(id, name, age, email, gender, address, salary) VALUES (null, ?, ?, ?, ?, ?, ?)";
        return jdbcTemplate.update(sql, employee.getName(), employee.getAge(), employee.getEmail(), employee.getGender(), employee.getAddress(), employee.getSalary());
    }

    @Override
    public int updateEmployee(Employee employee) {
        String sql = "UPDATE employee SET name = ?, age = ?, email = ?, gender = ?, address = ?, salary = ? WHERE id = ?";
        return jdbcTemplate.update(sql, employee.getName(), employee.getAge(), employee.getEmail(), employee.getGender(), employee.getAddress(), employee.getSalary(), employee.getId());
    }

    @Override
    public int deleteEmployeeById(long id) {
        String sql = "DELETE FROM employee WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public List<Employee> getAllEmployees() {
        String sql = "SELECT * FROM employee";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Employee.class));
    }
}
